#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define N 30

int n;
char maze[N][N];

typedef struct Position
{
    int x;
    int y;
}Pos,QElemType;

typedef int Status;

typedef struct QNode
{
    QElemType data;
    struct QNode *next;
}QNode, *QueuePtr;
typedef struct
{
    QueuePtr front;
    QueuePtr rear;
}LinkQueue;

int EscapePath(Pos source, Pos destination);

int main()
{
    scanf("%d\n", &n);
    for (int i = 0; i < n; i++)
        getline(maze[i]);
    
    Pos source, destination;
    scanf("%d %d", &source.x, &source.y);
    scanf("%d %d", &destination.x, &destination.y);
    
    printf("%d\n", EscapePath(source, destination));
    
    return 0;
}

/* 请在这里填写答案 */
struct {
    Pos now;
    int step;
}aaa[100000];
int EscapePath(Pos source, Pos destination)
{
    int head=0,tail=0;
    aaa[head].now=source;
    aaa[head].step=0;
    ++head;
    for(;head!=tail;)
    {
        Pos now=aaa[tail].now;
        int step=aaa[tail].step;
        ++tail;
        if(now.x==destination.x&&now.y==destination.y)
            return step;
        Pos tmp;
        tmp=now;++tmp.x;if(maze[tmp.x][tmp.y]!='#'){aaa[head].now=tmp;aaa[head].step=step+1;++head;maze[tmp.x][tmp.y]='#';}
        tmp=now;--tmp.x;if(maze[tmp.x][tmp.y]!='#'){aaa[head].now=tmp;aaa[head].step=step+1;++head;maze[tmp.x][tmp.y]='#';}
        tmp=now;++tmp.y;if(maze[tmp.x][tmp.y]!='#'){aaa[head].now=tmp;aaa[head].step=step+1;++head;maze[tmp.x][tmp.y]='#';}
        tmp=now;--tmp.y;if(maze[tmp.x][tmp.y]!='#'){aaa[head].now=tmp;aaa[head].step=step+1;++head;maze[tmp.x][tmp.y]='#';}
    }
    return -1;
}